﻿using System;
using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using DevComponents.WPF.Controls;
using DevComponents.WPF.Metro;
using Microsoft.Practices.Prism.Commands;
using ZhiHeng.Consume.DataContract;
using ZhiHeng.OneCard.WpfClient.Common;
using ZhiHeng.OneCard.WpfClient.Model;

using System.Linq;
using System.Linq.Expressions;

namespace ZhiHeng.OneCard.WpfClient.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public static MainViewModel Instance;

        public MainViewModel()
        {
            Instance = this;
            //Login = new LoginViewModel(this);
            //_LoggedInUser = new UserViewModel(this);
        }

        private bool _LoginStatus;

        public bool LoginStatus
        {
            get
            {
                return _LoginStatus;
            }
            set
            {
                SetPropertyValue(value, ref _LoginStatus, "LoginStatus");
                if (value)
                {
                    StartPages = CreateStartPages();
                }else
                {
                    ReturnToStart();
                }
            }
        }


        private MetroTheme _MetroTheme;
        public MetroTheme MetroTheme
        {
            get { return _MetroTheme; }
            set { SetPropertyValue(value, ref _MetroTheme, "MetroTheme"); }
        }

        private ApplicationViewModel _CurrentApplication;
        public ApplicationViewModel CurrentApplication
        {
            get { return _CurrentApplication; }
            set { SetPropertyValue(value, ref _CurrentApplication, "CurrentApplication"); }
        }

        private ObservableCollection<ApplicationViewModel> _Applications;
        public ObservableCollection<ApplicationViewModel> Applications
        {
            get
            {
                if (_Applications == null)
                    _Applications = new ObservableCollection<ApplicationViewModel>();
                return _Applications;
            }
            set
            {
                SetPropertyValue(value, ref _Applications, "Applications");
            }
        }

        private ObservableCollection<StartPageViewModel> _StartPages;
        public ObservableCollection<StartPageViewModel> StartPages
        {
            get
            {
                if (_StartPages == null)
                    _StartPages = new ObservableCollection<StartPageViewModel>();
                return _StartPages;
            }
            set
            {
                SetPropertyValue(value, ref _StartPages, "StartPages");
                //Applications = CreateApplicationsList();
                //LoginSliderState = SlidingContentControlState.Normal;
            }
        }
        public ObservableCollection<UserViewModel> Users { get; private set; }

        public LoginViewModel Login { get; private set; }

        private SlidingContentControlState _LoginSliderState;
        public SlidingContentControlState LoginSliderState
        {
            get { return _LoginSliderState; }
            set { SetPropertyValue(value, ref _LoginSliderState, "LoginSliderState"); }
        }

        private DelegateCommand _ReturnToStartCommand;
        public DelegateCommand ReturnToStartCommand
        {
            get
            {
                if (_ReturnToStartCommand == null)
                    _ReturnToStartCommand = new DelegateCommand(
                        () => ReturnToStart(),
                        () => CurrentApplication != null);
                return _ReturnToStartCommand;
            }
        }

        public void ReturnToStart()
        {
            if (CurrentApplication != null)
            {
                CurrentApplication.IsActive = false;
                CurrentApplication = null;
            }
        }

        private ObservableCollection<StartPageViewModel> CreateStartPages()
        {
            var pages = new ObservableCollection<StartPageViewModel>();
            var opLevelArr = UserInfo.operatorGroup.OperLevel.Split(',');
            foreach (var menu in FormUtility.GetMenus())
            { 
                var page1 = new StartPageViewModel();
                bool haveSubRight = false;
                page1.Applications.Add(CreateModule(menu));

                foreach (var subMenu in menu.Menus)
                {
                    if (!opLevelArr.Contains(subMenu.Id) ) continue;
                    haveSubRight = true;
                    try
                    {
                        var av = CreateApplicationViewModel(subMenu);
                        page1.Applications.Add(av);
                        Applications.Add(av);
                    }
                    catch (Exception ex1)
                    {
                        Console.WriteLine(ex1.Message);
                    }
                }

                if (haveSubRight)
                {
                    pages.Add(page1);
                }
            }
            return pages;
        }

        private static ApplicationViewModel CreateModule(Menu menu)
        {
            return new ApplicationViewModel
                       {
                           Title = menu.Name,
                           Description = "",
                           ImageSource =
                               new BitmapImage(new Uri(@"/" + menu.Icon,
                                                       UriKind.RelativeOrAbsolute)),
                           TileColor = menu.TileColor,
                           Width = menu.Width,
                           Height = menu.Height,
                           MetroTheme = DevComponents.WPF.Metro.MetroTheme.WashedWhite
                       };
        }

        private static ApplicationViewModel CreateApplicationViewModel(Menu subMenu)
        {
            var av = new ApplicationViewModel();
            av.Title = subMenu.Name;
            av.ImageSource =
                new BitmapImage(new Uri(@"/" + subMenu.Icon, UriKind.RelativeOrAbsolute));
            av.Width = subMenu.Width;
            av.Height = subMenu.Height;
            av.TileColor = subMenu.TileColor;
            av.MetroTheme = DevComponents.WPF.Metro.MetroTheme.WashedWhite;
            av.ViewPath = subMenu.Form;
            return av;
        }

        private ObservableCollection<ApplicationViewModel> CreateApplicationsList()
        {
            var apps = new ObservableCollection<ApplicationViewModel>();

            foreach (var page in StartPages)
            {
                foreach (var app in page.Applications)
                {
                    apps.Add(app);
                }
            }

            return apps;
        }

        private @operator _Userinfo;
        public @operator Userinfo
        {
            get
            {
                return UserInfo;
            }
            set 
            {
                //UserInfo = value;
                SetPropertyValue(value, ref _Userinfo, "UserInfo");
            }

        }
    }

}
